/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/10 23:26
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
(function () {

    "use strict";

    var api = {
        getSliderListsUrl: '/bbs/slider/lists',
        deleteData: '/bbs/slider/deleteSlider',
        changeStatus: '/bbs/slider/changeStatus'
    };

    sm.controller('admin-bbs-slider-index-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.lists = [];
        $scope.page = {
            page: 1,
            type: "all"
        };
        $scope.nextPage = false;

        $scope.getLists = function () {
            $http.get(api.getSliderListsUrl, {
                params: {
                    page: 1
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.nextPage = res.data.nextPage;
                    appendLists($scope.lists, res.data.data);
                }
            });
        };

        $scope.getLists();

        function appendLists(Lists, list) {
            if (list.length == 0) {
                return false;
            }
            angular.forEach(list, function (val) {
                Lists.push(val);
            });
        }

        $scope.changeStatus = function (index) {
            if (!$scope.lists[index]['id']) {
                return false;
            }
            Modal.alert({
                title: "您确定修改广告状态吗?",
                content: "状态一旦修改可能无法显示,请谨慎操作",
                buttons: [{
                    name: '取消',
                    type: 'cancel'
                }, {
                    name: '确定修改',
                    event: function () {
                        doChangeStatus(index);
                    }
                }]
            });
        };

        function doChangeStatus(index) {
            $http.get(api.changeStatus, {
                params: {
                    sliderId: $scope.lists[index]['id']
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.lists[index]['status'] = $scope.lists[index]['status'] == 1 ? 0 : 1;
                } else {
                    Modal.error('删除失败');
                }
            });
        }

        $scope.deleteItem = function (index) {
            if (!$scope.lists[index]['id']) {
                return false;
            }
            Modal.alert({
                title: "您确认删除该广告吗?",
                content: "广告一旦被删除将无法恢复,请谨慎操作",
                buttons: [{
                    name: '取消',
                    type: 'cancel'
                }, {
                    name: '确定删除',
                    event: function () {
                        doDelete(index);
                    }
                }]
            });
        };

        function doDelete (index) {
            $http.get(api.deleteData, {
                params: {
                    sliderId: $scope.lists[index]['id']
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.lists.splice(index, 1);
                } else {
                    Modal.error('删除失败');
                }
            });
        }


    }]);

})();